/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.joinhawaii.admin;

import java.util.*;

import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;

import com.joinhawaii.home.BoardDAO;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;




/**
 *
 * @author eastar
 */
public class WordSaveCommand implements Command
{
    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	String userid 				= StringUtil.nvl(request.getSession().getAttribute("session.userid"), "guest");
    	String pmenu_id				= StringUtil.nvl(request.getParameter("pmenu_id"));
    	String menu_id				= StringUtil.nvl(request.getParameter("menu_id"));

		String job_type 			= StringUtil.nvl(request.getParameter("job_type"));
		String board_id     		= StringUtil.nvl(request.getParameter("board_id"));
		String word_id     			= StringUtil.nvl(request.getParameter("word_id"));
    	String word_num     		= StringUtil.nvl(request.getParameter("word_num"));
		String subject      		= StringUtil.toHan(request.getParameter("subject"));
		String password         	= StringUtil.nvl(request.getParameter("password"));
		String open_yn      		= StringUtil.nvl(request.getParameter("open_yn"));
		String content      		= StringUtil.toHan(request.getParameter("content"));
		String author_name      	= StringUtil.toHan(request.getParameter("author_name"));
		String email		      	= StringUtil.toHan(request.getParameter("email"));
		String telephone			= StringUtil.nvl(request.getParameter("telephone"));
		String ip_addr         		= StringUtil.nvl(request.getParameter("ip_addr"));
		String sort         		= StringUtil.nvl(request.getParameter("sort"));
		String depth        		= StringUtil.nvl(request.getParameter("depth"));


		BoardDAO boardDAO = new BoardDAO();

		HashMap infos = new HashMap();

		infos.put("job_type", 		job_type 	);
		infos.put("word_id",		word_id);

		if ("C".equals(job_type)) {

			word_num 	= boardDAO.selectWordNum(board_id);

    		infos.put("board_id",     	board_id   	);
    		infos.put("word_num",     	word_num 	);
    		infos.put("subject",      	subject    	);
    		infos.put("password",     	password   	);
    		infos.put("open_yn",      	open_yn    	);
    		infos.put("content",      	content		);	//StringUtil.replaceContent(content, "img/pic/board/"+board_id));
    		infos.put("author_name",  	author_name	);
    		infos.put("email",			email		);
    		infos.put("telephone",		telephone	);
    		infos.put("ip_addr",      	request.getRemoteAddr());
    		infos.put("sort",         	"1"       	);
    		infos.put("depth",        	"0"      	);
    		infos.put("init_user",		userid		);
    		infos.put("up_user",		userid		);

		} else if ("R".equals(job_type)) {

    		infos.put("board_id",     	board_id   	);
    		infos.put("word_num",     	word_num   	);
    		infos.put("subject",      	subject    	);
    		infos.put("password",     	password   	);
    		infos.put("open_yn",      	open_yn    	);
    		infos.put("content",      	content		);	//StringUtil.replaceContent(content, "img/pic/board/"+board_id));
    		infos.put("author_name",  	author_name	);
    		infos.put("email",			email		);
    		infos.put("telephone",		telephone	);
    		infos.put("ip_addr",      	request.getRemoteAddr());
    		infos.put("sort",         	new Integer(Integer.parseInt(sort)+1)  	);
    		infos.put("depth",        	new Integer(Integer.parseInt(depth)+1) 	);
    		infos.put("init_user",		userid);
    		infos.put("up_user",		userid);

		} else if ("U".equals(job_type)) {

    		infos.put("board_id",     	board_id   	);
    		infos.put("subject",      	subject    	);
    		infos.put("password",     	password   	);
    		infos.put("open_yn",      	open_yn    	);
    		infos.put("content", 	 	content);//infos.put("content",      	StringUtil.replaceContent(content, "img/pic/board/"+board_id));
    		infos.put("author_name",  	author_name	);
    		infos.put("email",			email		);
    		infos.put("telephone",		telephone	);
    		infos.put("up_user",		userid		);
		}





		/* board info save */
		boolean bResult			= boardDAO.saveWord(infos);



		HashMap result		= new HashMap();

		if (bResult) {
			result.put("result", 	"true");
			//result.put("target",	target);
			result.put("action",	"./boardList.jsp?board_id="+board_id);

	    	request.setAttribute("result", result);

	    	return "/common/move_view.jsp";
	    } else {
	    	return "/common/error_page.jsp";
	    }
    }
}
